﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LineMeshCreater : MonoBehaviour
{

    public Mesh GetMesh(Vector3[] posarray, float width , Vector3 world, Vector3 nextNode = default, float UV_offset = 1)
    {
        Mesh mesh = new Mesh();
        DrawMesh(mesh, posarray, width , world , nextNode);
        return mesh;
    }

    /// <summary>
    /// 绘制Mesh
    /// </summary>
    /// <param name="mesh"></param>
    /// <param name="posarray"></param>
    /// <param name="width"></param>
    public void DrawMesh(Mesh mesh, Vector3[] posarray, float width, Vector3 world, Vector3 nextNode = default , float UV_offset = 1)
    {
        mesh.Clear();
        mesh.vertices = getVertices(posarray, width , world , nextNode);
        mesh.triangles = getTriangles(mesh.vertexCount / 2);
        mesh.uv = getUVs(posarray.Length, UV_offset);
        mesh.RecalculateNormals();
    }

    /// <summary>
    /// 顶点计算
    /// </summary>
    /// <returns></returns>
    Vector3[] getVertices(Vector3[] posarray, float width , Vector3 world , Vector3 nextNode = default)
    {
        Vector3[] vector3s = new Vector3[posarray.Length * 2 * 2 ];
        for (int i = 0; i < posarray.Length; i++)
        {
            Vector3 reght = Vector3.zero;

            if (i > 0 && i < posarray.Length - 1)
            {
                Vector3 t_l, t_r;
                t_l = t_r = Vector3.zero;
                //节点左边的叉乘向量
                getCross(posarray[i - 1] - posarray[i], world, ref t_l);
                //节点右边的叉乘向量
                getCross(posarray[i + 1] - posarray[i], world, ref t_r);
                //取均值
                t_r = -t_r;
                reght = (t_r + t_l) / 2;
            }
            else if (i == posarray.Length - 1)
            {
                if (nextNode != Vector3.zero)
                {
                    //到下一个节点的叉乘向量
                    getCross(nextNode - posarray[i], world, ref reght);
                    reght = -reght;
                }
                else {
                    //节点左边的叉乘向量
                    getCross(posarray[i - 1] - posarray[i], world, ref reght);
                }
            }
            else
            {
                //节点右边的叉乘向量
                getCross(posarray[i + 1] - posarray[i], world, ref reght);
                reght = -reght;
            }

            //右顶点
            vector3s[i * 2] = reght * (width / 2) + posarray[i] - posarray[0];
            //左顶点
            vector3s[i * 2 + 1] = -reght * (width / 2) + posarray[i] - posarray[0];

            //背面顶点
            vector3s[posarray.Length * 2 + i * 2] = reght * (width / 2) + posarray[i] - posarray[0] + world * -0.01f;

            vector3s[posarray.Length * 2 + i * 2 + 1] = -reght * (width / 2) + posarray[i] - posarray[0] + world * -0.01f;

            //Debug.DrawLine(vector3s[i * 2] + posarray[0], vector3s[i * 2 + 1] + posarray[0], Color.red);
        }
        return vector3s;
    }

    /// <summary>
    /// 生成UV坐标
    /// </summary>
    /// <param name="posarray">节点数</param>
    /// <param name="uvOffset">UV缩放系数</param>
    /// <returns></returns>
    Vector2[] getUVs(int posarray, float uvOffset = 1) {
        Vector2[] uvs = new Vector2[posarray * 2 * 2];
        //每个节点之间的uv平均值
        float uv_l = 1.0f / posarray / uvOffset;
        for (int i = 0; i < posarray; i++)
        {
            uvs[i * 2] = new Vector2(0 , i * uv_l);
            uvs[i * 2 + 1] = new Vector2(1, i * uv_l);
            uvs[posarray * 2 + i * 2] = new Vector2(0, i * uv_l);
            uvs[posarray * 2 + i * 2 + 1] = new Vector2(1, i * uv_l);
        }
        return uvs;
    }

    /// <summary>
    /// 叉乘
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="vector"></param>
    void getCross(Vector3 a, Vector3 b, ref Vector3 vector)
    {
        vector = new Vector3(
                a.y * b.z - a.z * b.y,
                a.z * b.x - a.x * b.z,
                a.x * b.y - a.y * b.x
            ).normalized;
    }

    /// <summary>
    /// 三角面计算
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    int[] getTriangles(int count)
    {
        List<int> vector3s = new List<int>();
        for (int i = 0; i < count - 2; i += 2)
        {
            //右下三角面
            int p0, p1, p2, p3;
            p0 = i;
            p1 = i + 1;
            p2 = i + 2;
            p3 = i + 3;
            //正面三角面
            vector3s.Add(p0);
            vector3s.Add(p1);
            vector3s.Add(p2);
            vector3s.Add(p1);
            vector3s.Add(p3);
            vector3s.Add(p2);
        }

        for (int i = 0; i < count - 2; i += 2)
        {
            //右下三角面
            int p0, p1, p2, p3;
            p0 = i;
            p1 = i + 1;
            p2 = i + 2;
            p3 = i + 3;
            //背面三角面
            vector3s.Add(count + p0);
            vector3s.Add(count + p2);
            vector3s.Add(count + p1);

            vector3s.Add(count + p1);
            vector3s.Add(count + p2);
            vector3s.Add(count + p3);
        }

        return vector3s.ToArray();
    }

}
